﻿#include "machinetypefun.h"

//(打开,关闭)
OutCtrlItem MachineTypeFun::creatOpenCloseItem(int macType, int showPriority, QString name, int def)
{
    return makeAOutCtrlItem(macType,showPriority,name,def,
                            "",false,-1,-1,-1,
                            "",false,-1,-1,-1,
                            "",false,-1,-1,-1,
                            openStr,true,OUTCONTROL_OPEN,-1,-1,
                            closeStr,true,OUTCONTROL_CLOSE,-1,-1);
}

   //(归零,正转,反转)
OutCtrlItem MachineTypeFun::creatZeroForwardReverseItem(int macType, int showPriority, QString name, int def)
{
   return makeAOutCtrlItem(macType,showPriority,name,def,
                                                 "",false,-1,-1,-1,
                                                 "",false,-1,-1,-1,
                                                 zeroStr,true,OUTCONTROL_ZERO,-1,-1,
                                                 jogPStr,true,-1,OUTCONTROL_JOGP,OUTCONTROL_STOP,
                                                 jogNStr,true,-1,OUTCONTROL_JOGN,OUTCONTROL_STOP);
}

//(上升,下降)
OutCtrlItem MachineTypeFun::creatAscendingDescendingItem(int macType, int showPriority, QString name, int def)
{
    return makeAOutCtrlItem(macType,showPriority,name,def,
                                                  "",false,-1,-1,-1,
                                                  "",false,-1,-1,-1,
                                                  "",false,-1,-1,-1,
                                                  ascendStr,true,OUTCONTROL_UP,-1,-1,
                            descendStr,true,OUTCONTROL_DOWN,-1,-1);
}
// 自定义名称- （使能）
OutCtrlItem MachineTypeFun::creatEnableItem(int macType, int showPriority, QString name, int def, QString btnName)
{
    return makeAOutCtrlItem(macType,showPriority,name,def,
                                                  "",false,-1,-1,-1,
                                                  "",false,-1,-1,-1,
                                                  "",false,-1,-1,-1,
                                                  "",false,-1,-1,-1,
                            btnName,true,OUTCONTROL_ENABLE,-1,-1);
}

//(归零,使能，失能，正转,反转)
OutCtrlItem MachineTypeFun::creatALLItem(int macType, int showPriority, QString name, int def)
{
    return makeAOutCtrlItem(macType,showPriority,name,def,
                            zeroStr,true,OUTCONTROL_ZERO,-1,-1,
                            enableStr,true,OUTCONTROL_ENABLE,-1,-1,
                            disableStr,true,OUTCONTROL_DISABLE,-1,-1,
                            jogPStr,true,-1,OUTCONTROL_JOGP,OUTCONTROL_STOP,
                            jogNStr,true,-1,OUTCONTROL_JOGN,OUTCONTROL_STOP);
}

//(使能，失能,正转,反转)
OutCtrlItem MachineTypeFun::creatNoZeroItem(int macType, int showPriority, QString name, int def)
{
    return makeAOutCtrlItem(macType,showPriority,name,def,
                            "",false,-1,-1,-1,
                            enableStr,true,OUTCONTROL_ENABLE,-1,-1,
                            disableStr,true,OUTCONTROL_DISABLE,-1,-1,
                            jogPStr,true,-1,OUTCONTROL_JOGP,OUTCONTROL_STOP,
                            jogNStr,true,-1,OUTCONTROL_JOGN,OUTCONTROL_STOP);
}
//(放松，收紧)
OutCtrlItem MachineTypeFun::creatReleaseTightenItem(int macType, int showPriority, QString name, int def)
{
    return makeAOutCtrlItem(macType,showPriority,name,def,
                            "",false,-1,-1,-1,
                            "",false,-1,-1,-1,
                            "",false,-1,-1,-1,
                            release,true,OUTCONTROL_CLOSE,-1,-1,
                            tighten,true,OUTCONTROL_OPEN,-1,-1);
}

//归零命令 显示转动
OutCtrlItem MachineTypeFun::creatZeroItem(int macType, int showPriority, QString name, int def)
{
    return makeAOutCtrlItem(macType,showPriority,name,def,
                            "",false,-1,-1,-1,
                            "",false,-1,-1,-1,
                            "",false,-1,-1,-1,
                            "",false,-1,-1,-1,
                            turnStr,true,OUTCONTROL_ZERO,-1,-1);
}

MachineTypeFun::MachineTypeFun(QObject *parent) : QObject(parent)
{
    m_allParasList.clear();
    m_allOutCtrlList.clear();
    m_allShortCutList.clear();
    m_allAssistList.clear();
    m_allPatternSetList.clear();
    m_allParaSetList.clear();
    m_allSensorList.clear();
    m_allErrorCodeAndStateList.clear();
    enableStr = QObject::tr("Enable");   //使能
    disableStr = QObject::tr("Disable"); //失能
    jogPStr = QObject::tr("JogP"); //正转
    jogNStr = QObject::tr("JogN"); //反转
    turnStr = QObject::tr("Turn"); //转动
    zeroStr = QObject::tr("Zero"); //归零
    openStr = QObject::tr("Open"); //打开
    closeStr = QObject::tr("Close");//关闭
    ascendStr = QObject::tr("Ascending");//上升
    descendStr = QObject::tr("Descending");//下降
    release = QObject::tr("Release");//放松
    tighten = QObject::tr("Tighten");//收紧
    trimStr = QObject::tr("Trim"); //剪线
    action = QObject::tr("Action"); //动作
    stop = QObject::tr("Stop"); //停止
    pulStr = QObject::tr("Pull"); //拉料
    backStr = QObject::tr("Back"); //退料
    stretchStr = QObject::tr("Stretch"); //伸出
    recoverStr = QObject::tr("Recover"); //收回
    stopStr  = QObject::tr("Stop"); //停止
    swingoneStr  = QObject::tr("Swinging position 1"); //摆动位1
    swingtwoStr  = QObject::tr("Swinging position 2"); //摆动位2
    runStr =  QObject::tr("Run"); //执行


    m_allMotorCtrlList.clear();
    m_allCylinderCtrlList.clear();

    initAllSensorList();
}

MachineTypeFun::~MachineTypeFun()
{
    m_allParasList.clear();
    m_allOutCtrlList.clear();
    m_allShortCutList.clear();
    m_allAssistList.clear();
    m_allPatternSetList.clear();
    m_allParaSetList.clear();
    m_allSensorList.clear();
    m_allErrorCodeAndStateList.clear();
}

//获取传感器列表
QList<SensorItem> MachineTypeFun::getSensorList()
{
    return m_allSensorList;
}

//获取错误代码列表
QList<ErrorCodeStateItem> MachineTypeFun::getErrorCodeAndStateList()
{
    return m_allErrorCodeAndStateList;
}

const QHash<int, NineItem> &MachineTypeFun::getNinebtn()
{
    return m_nineBtn;
}

const QList<PATTERNINFO> &MachineTypeFun::getFileInfoMSG()
{
    return m_fileInfo;
}

void MachineTypeFun::initFileInfoLabel()
{
    m_fileInfo.append(FileName);
    m_fileInfo.append(Index);
    m_fileInfo.append(BeginPoint);
    m_fileInfo.append(LocalPoint);
    m_fileInfo.append(Range);
    m_fileInfo.append(Position);
    m_fileInfo.append(Production);
}

void MachineTypeFun::initRootParasList()
{
    //超级用户参数---------------------------
    //超级用户参数-界面配置；单位：无
    m_allParasList.append(makeAParaItem(0,
                                        PARA_TYPE_ROOT,
                                        0 ,
                                        ROOT_HMICONFIGURATION, 0, 0, PARA_VALUE_TYPE_CLICK,
                                        tr("HMI Configuration"),//界面配置
                                        0, 0, 0, 0, 0, tr(""), tr(""),
                                        tr(""),
                                        0, 0, 0x0000 , root));

    //超级用户参数-界面进入调试模式；单位：无
    m_allParasList.append(makeAParaItem(0,
                                        PARA_TYPE_ROOT,
                                        0 ,
                                        ROOT_DEBUGMODE, 0, 0, PARA_VALUE_TYPE_CLICK,
                                        tr("Interface debug mode"),//界面进入调试模式
                                        0, 0, 0, 0, 0, tr(""), tr(""),
                                        tr(""),
                                        0, 0, 0x0001 , root));

    //超级用户参数-删除config.ini配置文件；单位：无
    m_allParasList.append(makeAParaItem(0,
                                        PARA_TYPE_ROOT,
                                        0 ,
                                        ROOT_DELETEINI, 0, 0, PARA_VALUE_TYPE_CLICK,
                                        tr("Delete configure file"),//删除配置文件
                                        0, 0, 0, 0, 0, tr(""), tr(""),
                                        tr(""),
                                        0, 0, 0x0001 ,  root));

    //超级用户参数-机器参数；单位：无
    m_allParasList.append(makeAParaItem(0,
                                        PARA_TYPE_ROOT,
                                        0 ,
                                        ROOT_MACHINEPARA, 0, 0, PARA_VALUE_TYPE_CLICK,
                                        tr("Machine parameters"),//机器参数
                                        0, 0, 0, 0, 0, tr(""), tr(""),
                                        tr(""),
                                        0, 0, 0x0002, root));

    //超级用户参数-工作参数；单位：无
    m_allParasList.append(makeAParaItem(0,
                                        PARA_TYPE_ROOT,
                                        0 ,
                                        ROOT_WORKPARA, 0, 0, PARA_VALUE_TYPE_CLICK,
                                        tr("Work parameters"),//工作参数
                                        0, 0, 0, 0, 0, tr(""), tr(""),
                                        tr(""),
                                        0, 0, 0x0003, root));
}

void MachineTypeFun::initAllSensorList()
{
    //所有传感器列表，字符为空
    m_allSensorList.clear();
    int addr, byteaddr, bitaddr;

    SensorItem item;

    for (addr = 0; addr < MAX_SENSOR_NUM; addr++)
    {
        byteaddr = addr / 8;
        bitaddr = addr % 8; //(0-7)
        item.setItemValue(byteaddr, bitaddr, "");
        m_allSensorList.append(item);
    }
}

QList<ParaSetItem> MachineTypeFun::getParaSetListByFilter()
{
    return m_allParaSetList;
}

QList<PatternSetItem> MachineTypeFun::getPatternSetListByFilter()
{
    return m_allPatternSetList;
}

QList<AssistItem> MachineTypeFun::getAssistListByFilter()
{
    return m_allAssistList;
}

QList<ShortCutItem> MachineTypeFun::getShortCutListByFilter()
{
    return m_allShortCutList;
}

QList<OutCtrlItem> MachineTypeFun::getOutCtrlListByFilter()
{
    return m_allOutCtrlList;
}

QList<ParaItem> MachineTypeFun::getParasListByMcSel()
{
    QList <ParaItem> paralist;
    int size = m_allParasList.size();

    for (int i = 0; i < size; i++)
    {
        const ParaItem & item = m_allParasList.at(i);
        paralist.append(item);
#if(0)
        if ((item.mcenSel & mcensel) != 0)
        {
            paralist.append(item);
        }
#endif
    }

    qStableSort(paralist.begin(), paralist.end(), comparePriority);
    return paralist;
}
